import axios from "axios";
import { ElMessage } from "element-plus";
const baseInstance = axios.create({
  baseURL: "", // 当前请求为跨域类型时是否在请求中协带cookie
  withCredentials: false,
  timeout: 1000,
});

// 请求拦截器
baseInstance.interceptors.request.use(
  (config) => {
    const { headers } = config;
    let setCode = sessionStorage.getItem("seatCode") || "";
    if (setCode) {
      headers.Authorization = "Basic " + window.btoa(setCode + ":" + "");
    } else {
      return ElMessage.error("请先输入坐席号");
    }
    return config;
  },

  (err) => {
    return Promise.reject(err);
  }
);

// 响应拦截器
baseInstance.interceptors.response.use(
  (res) => {
    return res;
  },

  (err) => {
    // const { data } = err.response;
    console.error(err);
    ElMessage.error(err);
  }
);

export default baseInstance;
